﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Cainos.PixelArtTopDown_Basic
{
    //when object exit the trigger, put it to the assigned layer and sorting layers
    //used in the stair objects for player to travel between layers
    public class LayerTrigger : MonoBehaviour
    {
        public string layer;
        public string sortingLayer;

        private void OnTriggerExit2D(Collider2D other)
        {
            if (other.tag == "Player" || other.tag=="Enemy"){
                other.gameObject.layer = LayerMask.NameToLayer(layer);
                SpriteRenderer spriteRendererToChange;
                spriteRendererToChange = other.gameObject.GetComponent<SpriteRenderer>();
                Debug.Log("First find"+spriteRendererToChange);
                if (spriteRendererToChange is null){
                    spriteRendererToChange = other.gameObject.GetComponentInChildren<SpriteRenderer>();
                }
                Debug.Log("Second find"+spriteRendererToChange);
                if (spriteRendererToChange is not null) {

                }
                SpriteRenderer[] srs = other.gameObject.GetComponentsInChildren<SpriteRenderer>();
                Debug.Log("Find"+srs.Length);
                foreach ( SpriteRenderer sr in srs)
                {
                    sr.sortingLayerName = sortingLayer;
                }
            }
        }

    }
}
